<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单区域 -->
      <el-form :model="user" :rules="rules" ref="user" class="login-form">
        <el-form-item prop="mobile">
          <el-input placeholder="用户名" v-model="user.mobile"></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input
            placeholder="密码"
            show-password
            v-model="user.code"
          ></el-input>
        </el-form-item>

        <el-form-item prop="isAgree">
          <el-checkbox-group v-model="user.isAgree">
            <el-checkbox
              label="我已阅读并同意用户协议和隐私条款"
              name="isAgree"
            ></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item>
          <el-button
            v-loading="user.loading"
            element-loading-text="拼命加载中"
            type="primary"
            class="login-btn"
            @click="login('user')"
            >立即登陆</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810',
        isAgree: [],
        loading: false
      },
      // 表单验证规则
      rules: {
        // 用户名
        mobile: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^1[345678]\d{9}$/,
            message: '请输入合法的用户名',
            trigger: 'blur'
          }
        ],
        // 密码
        code: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\d{6}$/,
            message: '请输入合法的密码',
            trigger: 'blur'
          }
        ],
        // 同意
        isAgree: [
          { required: true, message: '请同意', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async doLogin () {
      try {
        const { mobile, code } = this.user
        this.loading = true // true为加载
        const res = await this.$axios({
          method: 'post',
          url: 'mp/v1_0/authorizations',
          data: {
            mobile,
            code
          }
        })
        // 请求成功
        this.$message({
          message: '恭喜你登入成功',
          type: 'success'
        })
        //  保存token
        localStorage.setItem('tokenStr', res.data.data.token)
        // 跳转到主页
        this.$router.push('/')
        this.loading = false // false为不加载
      } catch (err) {
        console.log(err)
        this.$message({
          message: '恭喜你登入失败',
          type: 'warning'
        })
        this.loading = false
      }
    },
    //  doLogin(){
    //     const { mobile, code } = this.user
    //       this.loading = true // true为加载
    //     this.$axios({
    //         url: 'mp/v1_0/authorizations',
    //         method: 'post',
    //         data: {
    //           mobile,
    //           code
    //         }
    //       })
    //         .then((res) => {
    //           this.$message({
    //             message: '恭喜你登入成功',
    //             type: 'success'
    //           })
    //           this.loading = false // false为不加载
    //           // 跳转到主页
    //           this.$router.push('/')
    //           //  保存token
    //           localStorage.setItem('tokenStr', res.data.data.token)
    //         })
    //         .catch((res) => {
    //           this.$message({
    //             message: '恭喜你登入失败',
    //             type: 'warning'
    //           })
    //           this.loading = false
    //         })
    //  },
    // 表单提交
    login (myuser) {
      this.$refs[myuser].validate((valid) => {
        if (valid) {
          this.doLogin()
        } else {
          return false
        }
      })
    }
  }
}
</script>

// 样式
<style lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
